package com.jf.apply_car.service.impl;

import com.jf.apply_car.bean.Menu;
import com.jf.apply_car.mapper.MenuMapper;
import com.jf.apply_car.service.MenuService;
import com.jf.apply_car.util.LoginUtil;
import com.jf.apply_car.util.ResultJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuMapper menuMapper;

    public ResultJson getMenuList() {
//        /*查询条件实体*/
//        Menu s1 = new Menu();
//        s1.setPower(String.valueOf(roleId));
//        s1.setParentId(0);
//        /*查询第一级菜单*/
//        List<Menu> list = menuMapper.getAll(s1);
//        for (Menu m1 : list) {
//            Menu s2 = new Menu();
//            s2.setParentId(m1.getMenuId());
//            s2.setPower(String.valueOf(roleId));
//            /*查询二级菜单*/
//            List<Menu> cl1 = menuMapper.getAll(s2);
//
//            for (Menu m2 : cl1) {
//                Menu s3 = new Menu();
//                s3.setParentId(m2.getMenuId());
//                s3.setPower(String.valueOf(roleId));
//                /*查询按钮*/
//                List<Menu> cl2 = menuMapper.getAll(s2);
//                m2.setChildren(cl2);
//            }
//            m1.setChildren(cl1);
//        }
//        System.out.println(list);

        List<Menu> list = setMenuChildren(0, LoginUtil.getLoginRoleId());
        System.out.println(list);
        return ResultJson.success(list);
    }

    public ResultJson getPage() {
        return ResultJson.success(menuMapper.getAll(new Menu()));
    }


    private List<Menu> setMenuChildren(int parentId, int roleId) {
        List<Menu> list = menuMapper.getAll(new Menu(parentId, roleId));
        if (list.isEmpty()) {
            return list;
        }
        for (Menu m : list) {
            List<Menu> l = setMenuChildren(m.getMenuId(), roleId);
            m.setChildren(l);
        }
        return list;
    }
}
